/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "Mouse.h"
#include <logs/Log.h>

namespace liba
{
namespace input
{
namespace hard
{
namespace marmalade
{

Mouse::Mouse(const std::string & name)
:	mouse_x( 0 ),
	mouse_y( 0 ),
	old_mouse_x( 0 ),
	old_mouse_y( 0 ),
	left_button_down( false ),
	right_button_down( false ),
	mouse_name(name)
{}

Mouse::~Mouse()
{}

void Mouse::mouse_up( int key )
{
	std::string name = (key == 0) ? "Left Button" : "Right Button";
	Manager::add_event( Event( get_name(), name, false ) );
	if( key == 0 )
		left_button_down = false;
	else
		right_button_down = false;
}

void Mouse::mouse_down( int key )
{
	std::string name = (key == 0) ? "Left Button" : "Right Button";
	Manager::add_event( Event( get_name(), name, true ) );
	if( key == 0 )
		left_button_down = true;
	else
		right_button_down = true;
}

void Mouse::mouse_move( int x, int y )
{
	mouse_x = x;
	mouse_y = y;

	if( abs( mouse_x - old_mouse_x ) > 0.001 )
		Manager::add_event( Event( get_name(), "Axis_X", true ) );
	if( abs( mouse_y - old_mouse_y ) > 0.001 )
		Manager::add_event( Event( get_name(), "Axis_Y", true ) );

	old_mouse_x = mouse_x;
	old_mouse_y = mouse_y;
}

std::string Mouse::get_name()const
{
	return mouse_name;
}

double Mouse::get_axis_position( const std::string & axis_name )const
{
	if( axis_name == "Axis_X" )
		return mouse_x / width * 2 - 1;

	if( axis_name == "Axis_Y" )
		return mouse_y / height * 2 - 1;

	if( axis_name == "Left Button" )
	{
		return left_button_down;
	}

	if( axis_name == "Right Button" )
	{
		return right_button_down;
	}

	return 0;
}

Manager::AxisType Mouse::get_axis_type( const std::string & axis_name )const
{
	if( axis_name == "Axis_X" )
		return Manager::AXIS;

	if( axis_name == "Axis_Y" )
		return Manager::AXIS;

	if( axis_name == "Left Button" )
		return Manager::BUTTON;

	if( axis_name == "Right Button" )
		return Manager::BUTTON;

	return Manager::NONE;
}

void Mouse::resize(int height, int width)
{
	this->height = height;
	this->width = width;
}

} // namespace marmalade
} // namespace hard
} // namespace input
} // namespace liba


